#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

int count_characters(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("Could not open file");
        return -1;
    }

    int count = 0;
    char ch;
    while ((ch = fgetc(file)) != EOF) {
        count++;
    }

    fclose(file);
    return count;
}

int count_words(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("Could not open file");
        return -1;
    }

    int count = 0;
    char ch;
    int in_word = 0;
    while ((ch = fgetc(file)) != EOF) {
        if (isspace(ch)) {
            in_word = 0;
        } else if (!in_word) {
            in_word = 1;
            count++;
        }
    }

    fclose(file);
    return count;
}

int count_sentences(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("Could not open file");
        return -1;
    }

    int count = 0;
    char ch;
    while ((ch = fgetc(file)) != EOF) {
        if (ch == '.' || ch == '!' || ch == '?') {
            count++;
        }
    }

    fclose(file);
    return count;
}

int count_lines(const char *filename) {
    FILE *file = fopen(filename, "r");
    if (!file) {
        perror("Could not open file");
        return -1;
    }

    int count = 0;
    char ch;
    while ((ch = fgetc(file)) != EOF) {
        if (ch == '\n') {
            count++;
        }
    }

    fclose(file);
    return count;
}

void test_count_characters() {
    int result = count_characters("tests/sample.txt");
    printf("Testing count_characters... Expected: 833, Got: %d\n", result);
    assert(result == 833);
}

void test_count_words() {
    int result = count_words("tests/sample.txt");
    printf("Testing count_words... Expected: 157, Got: %d\n", result);
    assert(result == 157);
}

void test_count_sentences() {
    int result = count_sentences("tests/sample.txt");
    printf("Testing count_sentences... Expected: 19, Got: %d\n", result);
    assert(result == 19);
}

void test_count_lines() {
    int result = count_lines("tests/sample.txt");
    printf("Testing count_lines... Expected: 0, Got: %d\n", result);
    assert(result == 0);
}


int main(int argc, char *argv[]) {
    if (argc < 3) {
        printf("Usage: wc.exe [option] [filename]\n");
        return 1;
    }

    const char *option = argv[1];
    const char *filename = argv[2];

    if (strcmp(option, "-c") == 0) {
        printf("Character count: %d\n", count_characters(filename));
    } else if (strcmp(option, "-w") == 0) {
        printf("Word count: %d\n", count_words(filename));
    } else if (strcmp(option, "-s") == 0) {
        printf("Sentence count: %d\n", count_sentences(filename));
    } else if (strcmp(option, "-l") == 0) {
        printf("Line count: %d\n", count_lines(filename));
    }  else if (strcmp(option, "ts") == 0){
        test_count_characters();
        test_count_words();
        test_count_sentences();
        test_count_lines();
        printf("All tests passed successfully!\n");
    }else {
        printf("Invalid option. Use -c, -w, -s, -l, -ts.\n");
    }
    system("pause");
    char a;
    printf("输入任意字符退出程序：");
    scanf("%c",&a);

    return 0;
}
